package com.ace3l.legal.gwt.homeportal.mainpage.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.rpc.AsyncCallback;

import com.ace3l.legal.gwt.domainmodule.client.*;
import com.ace3l.legal.gwt.resourcemodule.client.PortalImageBundle;
import com.gwtext.client.widgets.*;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.core.EventObject;
import com.synesthesiaconsulting.gwt.resources.client.DirectorUnit;
import com.synesthesiaconsulting.gwt.resources.client.GWTActionUnitFactory;
import com.synesthesiaconsulting.gwt.resources.client.GWTUtils;

/**
 * ACE3L.COM
 * com.ace3l.legal.gwt.client
 * This code was written by Synesthesia Consulting Services in conjunction
 * with Ace3L.com. This code is confidential and proprietary. This code is
 * copyright (C) 2008  Synesthesia Consulting Services and Ace3L.com
 * Created By: ageer
 * Date: Jul 19, 2008
 * Time: 3:55:13 PM
 */
public class Ace3LPortal extends Ace3LPageTemplate {

    HorizontalPanel jumpBoardPanel = new HorizontalPanel();
    HTMLPanel infoPanel;
    VerticalPanel loginPanel = new VerticalPanel();
    DirectorUnit bodyPanelDirector;

    public void init() {
        infoPanelInit();
        loginPanelInit();
        jumpBoardPanelInit();

        director.addModule("homeportal.CreateLawFirm");
        director.addModule("homeportal.CreateStudent");
        director.addModule("lawfirmportal.LawFirmPortal");
        director.addModule("studentportal.StudentPortal");
        director.addModule("adminportal.AdminPortal");

        bodyPanelDirector = director.getDirector("homeportal.Ace3LPortal").addSubModule("bodyPanel");
        bodyPanelDirector.addAction("jumpBoard", GWTActionUnitFactory.makeGWTNavigationUnit(jumpBoardPanel, true));
        bodyPanelDirector.addAction("startPage", GWTActionUnitFactory.makeGWTNavigationUnit(getBodyPanelStart(), true));
        bodyPanelDirector.applyActionTo("startPage", bodyPanel);
    }

    private void jumpBoardPanelInit() {

        Button viewLawFirmPortal = new Button("View Law Firm Portal", new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                director.goToModule("lawfirmportal.LawFirmPortal");
            }
        });

        Button viewStudentPortal = new Button("View Student Portal", new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                director.goToModule("studentportal.StudentPortal");

            }
        });

        Button viewAdminPortal = new Button("View Admin Portal", new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                director.goToModule("adminportal.AdminPortal");
            }
        });

        jumpBoardPanel.add(viewLawFirmPortal);
        jumpBoardPanel.add(viewStudentPortal);
        jumpBoardPanel.add(viewAdminPortal);
    }

    private void infoPanelInit() {
        infoPanel = new HTMLPanel(
                "<p class=\"basicParagraph\">Thank you for visiting Ace3L.com. We are very excited about " +
                        "serving you with the most innovative and cost-efficient method " +
                        "for getting legal research done. Please take this opportunity " +
                        "to sign up for our mailing list below, and we will let you know " +
                        "when Ace3L is ready to serve.</p>" +
                        "<p class=\"basicParagraph\">Thanks again for visiting.</p>");

        infoPanel.setWidth("758px");

    }

    private void loginPanelInit() {
        loginPanel.setWidth("200px");

        final FormPanel formPanel = new FormPanel();
        formPanel.setFrame(true);
        formPanel.setTitle("Ace3L Login");
        formPanel.setLabelWidth(50);

        formPanel.setHeight(200);
        Label loginInfo = new Label("Sign Into Ace3L.com");
        loginInfo.setStyleName("login-label");
        formPanel.add(loginInfo);

        final TextField username = new TextField("Username", "username", 135);
        final TextField password = new TextField("Password", "password", 135);
        password.setPassword(true);
        formPanel.add(username);
        formPanel.add(password);
        ButtonListenerAdapter loginClick = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                String sUsername = username.getText();
                String sPassword = password.getText();
                final AsyncCallback jumpCallback = new AsyncCallback() {
                    public void onSuccess(Object result) {
                        String role = (String) result;
                        if (role.equals("LAWFIRM")) {
                            director.goToModule("lawfirmportal.LawFirmPortal");
                        } else  if (role.equals("STUDENT")) {
                           director.goToModule("studentportal.StudentPortal");
                        } else  if (role.equals("ADMIN")) {
                           director.goToModule("adminportal.AdminPortal");
                        }

                    }

                    public void onFailure(Throwable caught) {
                        Label loginFailed = new Label("Login failed.");
                        loginFailed.setStyleName("login-error");
                        loginPanel.add(loginFailed);
                    }
                };

                AsyncCallback authCallback = new AsyncCallback() {
                    public void onSuccess(Object result) {
                        Boolean isAuth = (Boolean) result;
                        if (isAuth) {
                            UserServiceAsync userService = UserService.App.getInstance();
                            userService.getRole(jumpCallback);
                        } else {
                            Label loginFailed = new Label("Login failed.");
                            loginFailed.setStyleName("login-error");
                            loginPanel.add(loginFailed);
                        }

                    }

                    public void onFailure(Throwable caught) {
                        Label loginFailed = new Label("Login failed.");
                        loginFailed.setStyleName("login-error");
                        loginPanel.add(loginFailed);
                    }
                };

                UserServiceAsync userService = UserService.App.getInstance();
                userService.authenticateUser(sUsername, sPassword, authCallback);
            }
        };
        Button login = new Button("Login",loginClick);
        ButtonListenerAdapter baNewLawFirm = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                director.goToModule("homeportal.CreateLawFirm");
            }
        };
        ButtonListenerAdapter baNewStudent = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                director.goToModule("homeportal.CreateStudent");
            }
        };
        Button newlaw = new Button("New Law Firm", baNewLawFirm);
        Button newStudent = new Button("New Student", baNewStudent);
        formPanel.add(login);
        formPanel.add(newlaw);
        formPanel.add(newStudent);
        loginPanel.add(formPanel);
    }

    private com.google.gwt.user.client.ui.Panel getBodyPanelStart() {
        VerticalPanel startPanel = new VerticalPanel();
        startPanel.setWidth("100%");

        FormPanel mailingPanel = new FormPanel();
        HorizontalPanel topPanel = new HorizontalPanel();

        topPanel.setWidth("100%");
        topPanel.add(infoPanel);
        topPanel.add(loginPanel);

        mailingPanel.setWidth(410);
        mailingPanel.setFrame(true);
        mailingPanel.setLabelWidth(140);
        //mailingPanel.setCls("centered");

        final Label signUp = new Label("We will contact you when Ace3L is ready to serve you.");
        signUp.setStyleName("login-label");
        final TextField name = new TextField("Name", "name", 250);
        final TextField email = new TextField("E-Mail", "email", 250);
        final Button doSignUp = new Button("Sign Me Up");

        doSignUp.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                String sName = name.getText();
                String sEmail = email.getText();
                AsyncCallback callback = new AsyncCallback() {
                    public void onSuccess(Object result) {

                        signUp.setText("Thank you for signing up.");
                        name.setVisible(false);
                        email.setVisible(false);
                        doSignUp.setVisible(false);

                    }

                    public void onFailure(Throwable caught) {
                        // do some UI stuff to show failure
                    }
                };
                MailingListServiceAsync ms = MailingListService.App.getInstance();
                ms.signUp(sName, sEmail, callback);
            }
        });
        mailingPanel.add(signUp);
        mailingPanel.add(name);
        mailingPanel.add(email);
        mailingPanel.add(doSignUp);


        startPanel.add(topPanel);
        startPanel.add(mailingPanel);
        return startPanel;

    }
}